package com.fyt.ZhongYaoApp.config;

import com.fyt.ZhongYaoApp.interceptors.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {

        registry.addMapping("/**")
                //允许所有源请求跨域，* 表示不限制
                .allowedOrigins("*")
                //允许所有原始请求头信息
                .allowedHeaders("*")
                //允许所有方法跨域，或者设置请求方式GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE
                .allowedMethods("GET", "POST", "DELETE", "PUT", "OPTIONS", "HEAD")
                //允许暴露哪些头信息，为了安全问题不能设置成*
                //.exposedHeaders();
                .maxAge(36000);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {


        // 登录接口和注册接口不拦截
        registry.addInterceptor(loginInterceptor)

                .excludePathPatterns("/user/login", "/user/register",
                        "/webjars/**", "/swagger-ui/**", "/v3/api-docs/**", "/swagger-ui.html","/doc.html","http://localhost:8080/swagger-ui/index.html#/" ,
                        "/zy/**" ,
                        "/essay/list","essay/find",
                        "/zycat/**",
                        "/doctor/**",
                        "/hospital/**",
                        "/doctorcat/**"
                        );

    }



}
